<template>

  <div class="app-container">

    <el-form ref="form" :model="form" label-width="80px">

      <el-form-item label="资讯标题">
        <el-input @input="getNameCount" v-model="form.title"></el-input>
        <span>{{nameCount}}/50 文字最多输入50字</span>
      </el-form-item>

      <el-form-item label="所属分类">
        <el-select v-model="form.classify_id" placeholder="请选择">
          <el-option
            v-for="item in classifies"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属学校">
        <el-select v-model="form.school_id" placeholder="请选择">
          <el-option
            v-for="item in school"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>



      <el-form-item label="概要">
        <el-input type="textarea" v-model="form.description"></el-input>
        <el-button @click="getBefore100()" type="text">获取内容前100字</el-button>
      </el-form-item>

      <el-form-item label="标签">
        <el-input v-model="form.sign" placeholder="两个标签之间用，隔开"></el-input>
      </el-form-item>
      <el-form-item label="关键字">
        <el-input v-model="form.key_word" placeholder="作用于SEO"></el-input>
      </el-form-item>


      <el-form-item label="封面">
        <el-upload
          class="avatar-uploader"
          :action="host+'/guard/upload?module=article'"
          accept=".png,.jpg,.jpeg"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>

        </el-upload>
        <span style="color: red">建议图片大小:275×154</span>
      </el-form-item>

      <el-form-item label="内容">
        <div id="div1">
        </div>
      </el-form-item>

      <el-form-item label="上架/下架">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="form.status"
          active-text="上架"
          inactive-text="下架">
        </el-switch>
      </el-form-item>

      <el-form-item label="是否置顶">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="form.is_top"
          active-text="是"
          inactive-text="否">
        </el-switch>
      </el-form-item>

      <el-form-item label="">
        <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
         <el-button @click="review('form')" >预览</el-button>
      </el-form-item>

    </el-form>

  </div>


</template>

<script>
import { myGet, myPost } from '@/utils/myRequest'
import E from 'wangeditor'
const urlArticleClassifyLst = '/guard/article/classify/lst'
const urlArticleAdd = '/guard/article/add'
const urlArticleAddReview = '/guard/article/addReview'
const urlSchool = "/guard/school/lst"
export default {

  data() {
    return {
      host:process.env.VUE_APP_HOST,
      nameCount:0,
      classifies:[],
      school:[],
      imageUrl:'',
      editor:null,
      form:{
        title:'',
        classify_id:'',
        school_id:0,
        key_word:'',
        sign:'',
        description:'',
        cover_url:'',
        status:1,
        is_top:2,
        content:''
      },
    };
  },
  methods:{

    review(formName) {
      this.$refs[formName].validate((valid)=>{
        if(valid){
          this.form.content = this.editor.txt.html()
          myPost(urlArticleAddReview,this.form).then(res => {
            console.log(res)
            if (res.status) {
              const id = res.data.records.id
              const url = "https://www.huiyouint.com/news/detail?id="+id
              console.log(url)
              window.open(url)
            }
          })
        }
      })
    },

    getBefore100(){
      this.form.description = this.editor.txt.text().substring(0,100)
    },
    getNameCount(val){
      this.nameCount = val.length
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid)=>{
        if(valid){
          this.form.content = this.editor.txt.html()
          myPost(urlArticleAdd,this.form).then(res => {
            if (res.status) {
              this.$message.success(res.msg)
              this.$router.push('/article/lst')
            }
          })
        }
      })
    },
    handleAvatarSuccess(res, file) {
      if(res.status){
        this.imageUrl = res.data.records.url
        this.form.cover_url = res.data.records.filePath
      }else{
        this.$message.error('上传失败!')
      }
    },
    beforeAvatarUpload(file) {
      /*const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;*/
    },
    closeAddDialog(){
      Object.assign(this.form ,this.$options.data().form)
    },

    getText(){
      console.log("change 之后最新的 html", this.editor.txt.text());
    },

    initEditor(){
      this.editor = new E('#div1')
      this.editor.config.uploadImgServer = this.host+ '/guard/article/uploadImg'
      this.editor.config.uploadVideoServer = this.host+ '/guard/article/uploadVideo'
      /*let editor =  this.editor
      let form = this.form
      this.editor.config.onchange = function (newHtm) {
        form.description = editor.txt.text().slice(0,99)
      }*/
      this.editor.create()
    }
  },


  mounted() {
    this.initEditor()
    myGet(urlArticleClassifyLst).then(res=>{
      if(res.status){
        this.classifies =[]
        res.data.records.forEach(item=>{
          this.classifies.push({value: item.id, label: item.name})
        })
      }
    })

    myGet(urlSchool).then(res=>{
      if(res.status){
        this.school =[{value: 0, label: '无'}]
        res.data.records.forEach(item=>{
          this.school.push({value: item.id, label: item.name})
        })
      }
    })
  }
}
</script>

<style >

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
